/**
* @project admin-go-api
* @name sysLoginInfo
* @descriptions 登录日志服务层
* @author 寒锋
* @date 2023-12-22 14:26:41
 */

package service

import (
	"admin-go-api/api/dao"
	"admin-go-api/api/entity"
	"admin-go-api/common/response"
	"admin-go-api/common/util"
	"admin-go-api/constants"
	"fmt"
	"github.com/gin-gonic/gin"
)

type ISysLoginInfoService interface {
	GetSysLoginInfoList(c *gin.Context, PageNum, PageSize, LoginStatus int, Username, BeginTime, EndTime string)
	CreateSysLoginInfo(c *gin.Context, Username, Message string, LoginStatus int)
	DeleteSysLoginInfoById(c *gin.Context, dto entity.SysLoginInfoIdDto)
	BatchDeleteSysLoginInfo(c *gin.Context, dto entity.DelSysLoginInfoDto)
	CleanSysLoginInfo(c *gin.Context)
}

type SysLoginInfoServiceImpl struct {
}

// GetSysLoginInfoList 获取日志列表
func (s SysLoginInfoServiceImpl) GetSysLoginInfoList(c *gin.Context, PageNum, PageSize, LoginStatus int, Username, BeginTime, EndTime string) {
	if PageSize < 1 {
		PageSize = 10
	}
	if PageNum < 1 {
		PageNum = 1
	}
	list, count := dao.GetSysLoginInfoList(PageNum, PageSize, LoginStatus, Username, BeginTime, EndTime)
	data := map[string]interface{}{"total": count, "pageNum": PageNum, "pageSize": PageSize, "rows": list}
	response.Success(c, data)
}

// CreateSysLoginInfo 添加登录日志
func (s SysLoginInfoServiceImpl) CreateSysLoginInfo(c *gin.Context, Username, Message string, LoginStatus int) {
	ip := c.ClientIP()
	loginLocation := util.GetRealAddressByIP(ip)
	browser := util.GetBrowser(c)
	os := util.GetOs(c)
	dao.CreateSysLoginInfo(Username, ip, loginLocation, browser, os, Message, LoginStatus)
}

// DeleteSysLoginInfoById 删除日志
func (s SysLoginInfoServiceImpl) DeleteSysLoginInfoById(c *gin.Context, dto entity.SysLoginInfoIdDto) {
	result := dao.DeleteSysLoginInfoById(dto)
	if result {
		response.Success(c, nil)
		return
	}
	response.Failed(c, int(constants.ApiCode.FAILED), fmt.Sprintf("删除失败"))
}

// BatchDeleteSysLoginInfo 批量删除
func (s SysLoginInfoServiceImpl) BatchDeleteSysLoginInfo(c *gin.Context, dto entity.DelSysLoginInfoDto) {
	result := dao.BatchDeleteSysLoginInfo(dto)
	if result {
		response.Success(c, nil)
		return
	}
	response.Failed(c, int(constants.ApiCode.FAILED), fmt.Sprintf("删除失败"))
}

// CleanSysLoginInfo 清空日志
func (s SysLoginInfoServiceImpl) CleanSysLoginInfo(c *gin.Context) {
	result := dao.CleanSysLoginInfo()
	if result {
		response.Success(c, nil)
		return
	}
	response.Failed(c, int(constants.ApiCode.FAILED), fmt.Sprintf("清空日志失败"))
}

var sysLoginInfoService = SysLoginInfoServiceImpl{}

func SysLoginInfoService() ISysLoginInfoService {
	return &sysLoginInfoService
}
